package mta.command.merge;

import mta.command.CommandIF;
import mta.io.LocalizationPackageWriter;
import mta.localization.LocalizationPackage;
import mta.localization.ModulePhrase;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class Merge implements CommandIF {
    private LocalizationPackage patchedPackage;
    private LocalizationPackage appliedPackage;
    private LocalizationPackageWriter packageWriter;

    public Merge(LocalizationPackage patchedPackage, LocalizationPackage appliedPackage, LocalizationPackageWriter packageWriter) {
        this.patchedPackage = patchedPackage;
        this.appliedPackage = appliedPackage;
        this.packageWriter = packageWriter;
    }

    public void execute() {
        for (ModulePhrase untranslatedPhrase : patchedPackage.getUntranslatedModulePhrases()) {
            String translation = null;

            if (appliedPackage.hasTranslationForModulePhrase(untranslatedPhrase)) {
                translation = appliedPackage.getTranslationForModulePhrase(untranslatedPhrase);
            } else if (appliedPackage.hasTranslation(untranslatedPhrase.getPhrase())) {
                translation = appliedPackage.getTranslation(untranslatedPhrase.getPhrase());
            }

            if (translation != null) {
                patchedPackage.setTranslationForModulePhrase(untranslatedPhrase, translation);
            }
        }
        packageWriter.updatePackage(patchedPackage);
    }

}
